package com.tc.util;

public class DataUtil {
	static public int bytesValue(int myInt, byte[] data, int offset) {

		int hexBase = 0xff; // A byte of all ones
		data[offset + 0] = (byte) (hexBase & myInt);
		data[offset + 1] = (byte) (((hexBase << 8 & myInt)) >> 8);
		data[offset + 2] = (byte) (((hexBase << 16) & myInt) >> 16);
		data[offset + 3] = (byte) (((hexBase << 24) & myInt) >> 24);
		return 4;
	}
	static public int bytesValue(short myInt, byte[] data, int offset) {
		
		int hexBase = 0xff; // A byte of all ones
		data[offset + 0] = (byte) (hexBase & myInt);
		data[offset + 1] = (byte) (((hexBase << 8 & myInt)) >> 8);
		return 2;
	}
	static public int bytesValue(long myInt, byte[] data, int offset) {

		long hexBase = 0xff; // A byte of all ones
		data[offset + 0] = (byte) (hexBase & myInt);
		data[offset + 1] = (byte) (((hexBase << 8 & myInt)) >> 8);
		data[offset + 2] = (byte) (((hexBase << 16) & myInt) >> 16);
		data[offset + 3] = (byte) (((hexBase << 24) & myInt) >> 24);
		data[offset + 4] = (byte) (((hexBase << 32) & myInt) >> 32);
		data[offset + 5] = (byte) (((hexBase << 40) & myInt) >> 40);
		data[offset + 6] = (byte) (((hexBase << 48) & myInt) >> 48);
		data[offset + 7] = (byte) (((hexBase << 56) & myInt) >> 56);
		return 8;
	}
	static public byte[] bytesValue(int v) {
		byte[] bytes = new byte[4];
		bytesValue(v, bytes, 0);
		return bytes;
	}
	static public byte[] bytesValue(short v) {
		byte[] bytes = new byte[2];
		bytesValue(v, bytes, 0);
		return bytes;
	}
	static public byte[] bytesValue(long v) {
		byte[] bytes = new byte[8];
		bytesValue(v, bytes, 0);
		return bytes;
	}
	static public long longValue(byte[] bytes){
		return longValue(bytes,0);
	}
	static public long longValue(byte[] bytes, int offset) {
		long hexBase = 0xff;
		long myInt = (bytes[offset + 7] & hexBase);
		System.out.println(Long.toHexString(myInt));
		myInt = (myInt << 8) | (bytes[offset + 6] & hexBase);System.out.println(Long.toHexString(myInt));
		myInt = (myInt << 8) | (bytes[offset + 5] & hexBase);System.out.println(Long.toHexString(myInt));
		myInt = (myInt << 8) | (bytes[offset + 4] & hexBase);System.out.println(Long.toHexString(myInt));
		myInt = (myInt << 8) | (bytes[offset + 3] & hexBase);System.out.println(Long.toHexString(myInt));
		myInt = (myInt << 8) | (bytes[offset + 2] & hexBase);System.out.println(Long.toHexString(myInt));
		myInt = (myInt << 8) | (bytes[offset + 1] & hexBase);System.out.println(Long.toHexString(myInt));
		myInt = (myInt << 8) | (bytes[offset + 0] & hexBase);
		System.out.println(Long.toHexString(myInt));
		return myInt;
	}
	static public int intValue(byte[] bytes){
		return intValue(bytes,0);
	}
	static public int intValue(byte[] bytes, int offset) {
		int hexBase = 0xff;
		int myInt = (bytes[offset + 3] & hexBase);
	
		myInt = (myInt << 8) | (bytes[offset + 2] & hexBase);
		myInt = (myInt << 8) | (bytes[offset + 1] & hexBase);
		myInt = (myInt << 8) | (bytes[offset + 0] & hexBase);
		return myInt;
	}
	static public short shortValue(byte[] bytes) {

		return shortValue(bytes, 0);
	}
	static public short shortValue(byte[] bytes, int offset) {
		short hexBase = 0xff;
		short myInt = bytes[offset + 1];
		myInt = (short)((myInt << 8) | (bytes[offset]& hexBase));
		return myInt;
	}
	private static String hexString="0123456789ABCDEF";
	static public String hex(byte[] buf){
		StringBuilder sb=new StringBuilder(buf.length*2);
		
		for(int j=buf.length - 1;j >=0 ;j--)
		{
			sb.append(hexString.charAt((buf[j]&0xf0)>>4));
			sb.append(hexString.charAt((buf[j]&0x0f)>>0));
			
	
		}
		return sb.toString();
	}
	static public String hex(byte[] buf, int off, int len){
		StringBuilder sb=new StringBuilder(len*2);
		
		for(int j= off + len; j >=off ;j--)
		{
			sb.append(hexString.charAt((buf[j]&0xf0)>>4));
			sb.append(hexString.charAt((buf[j]&0x0f)>>0));
			
	
		}
		return sb.toString();
	}
	static public String hex(short v){
		
		return hex(bytesValue(v));
	}
	static public String hex(int v){
		
		return hex(bytesValue(v));
	}
static public String hex(byte v){
	StringBuilder sb=new StringBuilder(2);
	sb.append(hexString.charAt((v&0xf0)>>4));
	sb.append(hexString.charAt((v&0x0f)>>0));

	

	return sb.toString();
	}
static public void main(String[] ss){
	byte[] d = bytesValue((long)-239);
	long v = longValue(d);
}
}
